<template>
  <div>
    <Modal
      v-model="show"
      :title="title"
      :mask="false"
      :closable="false"
      :mask-closable="false"
      :width="400"
      draggable
      scrollable
      @on-visible-change="onVisibleChange"
    >
      <Scroll>
        <Tree v-if="show" ref="tree" :data="resource"  :empty-text="emptyTextTree" show-checkbox multiple @on-check-change="nodeClick" />
        <Spin v-if="laodingTree" size="large" />
      </Scroll>
      <div slot="footer">
        <Button @click="btn_clickCancel">取消</Button>
        <Button type="primary" :loading="loadingSubmit" @click="btn_clickSubmit">提交</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { getRolePermissions, rolePermissionEdit } from '@/api/home_'
import Utils from '@/utils/util'
export default {
  name: 'DataModalPerm',
  props: {
    // data: { type: Object, default: null }
  },
  data() {
    return {
      roleId: '',
      show: false,

      loadingSubmit: false,
      laodingTree: false,
      resource: [],
      emptyTextTree: '',
      nresIdList:[],
      nodeList: []
    }
  },
  computed: {
    title() {
      return '勾选资源分配权限'
    }
  },
  mounted() {},
  methods: {
    nodeClick(v) {
      this.nodeList = JSON.parse(JSON.stringify(v))
    },
    formatOptionsList(data,parent){
      data.forEach(item => {
        var children = item.children;
        if(children.length>0){
          this.formatOptionsList(children,item);
        }
        if(item.checked==0){
          if(parent != null){
            parent.checked = 0;
          }
          return;
        }
      });
    },
    showDialog(v) {
      this.resource = []
      this.roleId = JSON.parse(v).roleId
      this.show = true
      getRolePermissions({
        role_id: JSON.parse(v).roleId
      }).then(res => {
        if (res.code == 200) {
          const newData = Utils.sortByOrder(res.data)
          this.formatOptionsList(newData,null)
          this.resource = newData
        }
      })
    },
    // 当窗体可见状态处理事件
    onVisibleChange(v) {

    },
    // 按钮事件-取消
    btn_clickCancel() {
      this.show = false
    },
    getIds(ids,navList){
      for(let i = 0;i<navList.length;i++){
        if(ids.includes(navList[i].id)){ 
          this.nresIdList.push(navList[i].id)
          if(navList[i].parentId!=0){
            this.nresIdList.push(navList[i].parentId)
          }
        }
        this.getIds(ids,navList[i].children)
      }
   
    },
    // 按钮事件-提交
    btn_clickSubmit() {
      this.nresIdList = []
      let ids = []
      let nodeLen = this.$refs.tree.getCheckedAndIndeterminateNodes()
      if (nodeLen.length == 0) {
        ids = []
      } else {
          nodeLen.forEach((item, index) => {
          ids.push(item.id)
        })
      }
      rolePermissionEdit({
        permissions: ids,
        roleId: this.roleId
      }).then(res => {
        if (res.code == 200) {
          this.show = false
          this.$message.success('设置成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</script>

<style></style>
